package com.duowei.diandan.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.duowei.diandan.R;
import com.duowei.diandan.app.App;

public class AnimHelper {
    @SuppressLint("StaticFieldLeak")
    private static AnimHelper INSTANCE;
    private Context mContext;

    private int[] mCartIconPosition = new int[2];

    private FrameLayout mAnimLayout;

    // --
    //动画时间
    private static final int AnimationDuration = 500;
    //正在执行的动画数量
    private int number = 0;
    private Boolean isClean = false;

    private AnimHelper() {
        mContext = App.getApp();
    }

    public static AnimHelper getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new AnimHelper();
        }

        return INSTANCE;
    }

    public void setLastLocationView(View view) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                view.getLocationInWindow(mCartIconPosition);
            }
        });
    }

    public void start(Window window, ImageView iv, int[] positions) {
        mCartIconPosition = positions;
        //
        ViewGroup rootView = (ViewGroup) window.getDecorView();
        mAnimLayout = rootView.findViewById(R.id.animLayout);
        if (mAnimLayout == null) {
            mAnimLayout = createAnimLayout(rootView);
        }

        //
        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.anim_goods);
        iv.startAnimation(animation);

        int[] startLocation = new int[2];
        iv.getLocationInWindow(startLocation);
        startLocation[0] = startLocation[0] + iv.getWidth() / 2;
        startLocation[1] = startLocation[1] + iv.getHeight() / 2;

        Drawable drawable = iv.getDrawable();

        doAnim(drawable.mutate(), startLocation);
    }

    private FrameLayout createAnimLayout(ViewGroup rootViewGroup) {
        FrameLayout animLayout = new FrameLayout(mContext);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setBackgroundResource(android.R.color.transparent);
        animLayout.setId(R.id.animLayout);
        rootViewGroup.addView(animLayout);

        return animLayout;
    }

    private void doAnim(Drawable drawable, int[] startLocation) {
        if (!isClean) {
            setAnim(drawable, startLocation);
        } else {
            try {
                mAnimLayout.removeAllViews();
                isClean = false;
                setAnim(drawable, startLocation);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                isClean = true;
            }
        }
    }

    /**
     * 动画效果设置
     *
     * @param drawable      将要加入购物车的商品
     * @param startLocation 起始位置
     */
    private void setAnim(Drawable drawable, int[] startLocation) {
        //缩放动画效果
        Animation mScaleAnimation = new ScaleAnimation(1.5f, 0.0f, 1.5f, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.1f, Animation.RELATIVE_TO_SELF, 0.1f);
        mScaleAnimation.setDuration(AnimationDuration);
        mScaleAnimation.setFillAfter(true);
        final ImageView iview = new ImageView(mContext);
        iview.setImageDrawable(drawable);
        final View view = addViewToAnimLayout(iview, startLocation);
        view.setAlpha(1f);//透明度

        //曲线运动
        int endX = mCartIconPosition[0] - startLocation[0];
        int endY = mCartIconPosition[1] - startLocation[1];
        Animation mTranslateAnimation = new TranslateAnimation(0, endX, 0, endY);
        //旋转动画效果
        Animation mRotateAnimation = new RotateAnimation(360, 0,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mRotateAnimation.setDuration(AnimationDuration);
        mTranslateAnimation.setDuration(AnimationDuration);

        AnimationSet mAnimationSet = new AnimationSet(true);
        mAnimationSet.setFillAfter(true);
        mAnimationSet.addAnimation(mRotateAnimation);
        mAnimationSet.addAnimation(mScaleAnimation);
        mAnimationSet.addAnimation(mTranslateAnimation);
        mAnimationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                number++;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                number--;
                if (number == 0) {
                    isClean = true;
                    if (mAnimLayout != null) {
                        mAnimLayout.removeAllViews();
                    }
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        view.startAnimation(mAnimationSet);
    }

    /**
     * 将要执行动画的view 添加到动画层
     *
     * @param view     要运行动画的View
     * @param location 动画的起始位置
     */
    private View addViewToAnimLayout(View view, int[] location) {
        int x = location[0];
        int y = location[1];
        mAnimLayout.addView(view);
//        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
//                dip2px(mCartIconPosition[0]), dip2px(mCartIconPosition[0]));
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                dip2px(48), dip2px(48));
        lp.leftMargin = x;
        lp.topMargin = y;
        view.setPadding(5, 5, 5, 5);
        view.setLayoutParams(lp);
        return view;
    }

    /**
     * dip，dp转化成px 用来处理不同分辨路的屏幕
     *
     * @param dpValue
     */
    private int dip2px(float dpValue) {
        float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
